package com.school.fuyuan.business.async;

import com.rabbitmq.client.Channel;
import com.school.fuyuan.business.mapper.InfoManageMapper;
import com.school.fuyuan.business.model.po.InfoManage;
import com.school.fuyuan.common.util.ParameterUtils;
import com.school.fuyuan.framework.config.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author zrhong
 * @version 1.0
 * @date 2019/3/19 23:38
 * @description 处理预约提醒
 */
@Component
public class InfoMangeHandler {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private final InfoManageMapper infoManageMapper;

    public InfoMangeHandler(InfoManageMapper infoManageMapper) {
        this.infoManageMapper = infoManageMapper;
    }

    @RabbitListener(queues = {RabbitConfig.ACTUAL_INFO_QUEUE_NAME})
    public void listenerDelayQueue(int id, Message message, Channel channel) {
        log.info("[listenerDelayQueue 监听的消息] - [消费时间]- [{}]- [id]  - [{}]", LocalDateTime.now(), id);
        try {
            InfoManage infoManage = infoManageMapper.getInfoManage(id);
            if (!ParameterUtils.isEmpty(infoManage)) {
                String followPersonId = infoManage.getFollowPersonId();
                infoManageMapper.insertApptInfo(id, followPersonId);
            }

            // TODO 通知 MQ 消息已被成功消费,可以ACK了
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.info("listenerDelayQueue错误：e-{}",e.getMessage());
            // TODO 如果报错了,那么我们可以进行容错处理,比如转移当前消息进入其它队列
        }
    }
}
